package com.cetcs.kmga.dataManager.entity.dataManager.vo;



import com.cetcs.kmga.dataManager.entity.device.SecuRule;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 *
 * Created by root on 2017/9/25.
 */
public class SecuRuleAddVo {

    //告警基础规则
    private SecuRule secuRuleInfo;

    //告警数据
    //告警条件
    private List<SecuRuleAlertCondition> alertConditions;

    //操作步骤信息
    private String info;

    /**
     * 验证输入
     */
    public String checkInput(){
        String msg = null;
        if(StringUtils.isEmpty(secuRuleInfo.getName())){
            msg = "告警名称不能为空";
        }
        if(StringUtils.isEmpty(secuRuleInfo.getStat())){
            msg = "规则状态不能为空";
        }
        if(StringUtils.isEmpty(secuRuleInfo.getLevel())){
            msg = "风险级别不能为空";
        }
        //时间格式:时:分
        String timeRegex = "[0-2][0-9]:[0-5][0-9]";
        if(alertConditions != null){
            for(SecuRuleAlertCondition condition : alertConditions){
                if(StringUtils.isEmpty(condition.getDayStart())){
                    condition.setDayStart("00:00");
                }
                if(StringUtils.isEmpty(condition.getDayEnd())){
                    condition.setDayEnd("23:59");
                }
                if(!StringUtils.isEmpty(condition.getDayStart()) && !condition.getDayStart().matches(timeRegex)){
                    msg = condition.getDayStart() + "不是正确的时间格式";
                    break;
                }
                if(!StringUtils.isEmpty(condition.getDayEnd()) && !condition.getDayEnd().matches(timeRegex)){
                    msg = condition.getDayEnd() + "不是正确的时间格式";
                    break;
                }

                if(!condition.checkTime()){
                    msg =  "开始时间应该晚于结束时间";
                    break;
                }
            }
        }
        //邮箱格式验证
        String emailRegex ="^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        if(!StringUtils.isEmpty(secuRuleInfo.getReceiver())){
            String[] emails = secuRuleInfo.getReceiver().split(",");
            for(String email : emails){
                if(!email.matches(emailRegex)){
                    msg = email + " 格式不正确";
                    break;
                }
            }
        }
        return msg;
    }
    public SecuRule getSecuRuleInfo() {
        return secuRuleInfo;
    }

    public void setSecuRuleInfo(SecuRule secuRuleInfo) {
        this.secuRuleInfo = secuRuleInfo;
    }

    public List<SecuRuleAlertCondition> getAlertConditions() {
        return alertConditions;
    }

    public void setAlertConditions(List<SecuRuleAlertCondition> alertConditions) {
        this.alertConditions = alertConditions;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}
